package MapReduce.PartitionerAndWriteComparable;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * 1. 定义类实现Writable接口
 * 2. 重写序列化和反序列化方法
 * 3. 重写空参构造
 * 4. toString方法
 */
public class FlowBean implements WritableComparable<FlowBean> {
    private long upFlow;  //上传流量
    private long downFlow; //下载流量
    private long sumFlow;   //总流量

    public FlowBean() {
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {

        dataOutput.writeLong(upFlow);
        dataOutput.writeLong(downFlow);
        dataOutput.writeLong(sumFlow);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {

        this.upFlow = dataInput.readLong();
        this.downFlow = dataInput.readLong();
        this.sumFlow = dataInput.readLong();
    }

    @Override
    public String toString() {
        return  upFlow  +  "\t" + downFlow +"\t" + sumFlow ;
    }

    public long getUpFlow() {
        return upFlow;
    }

    public void setUpFlow(long upFlow) {
        this.upFlow = upFlow;
    }

    public long getDownFlow() {
        return downFlow;
    }

    public void setDownFlow(long downFlow) {
        this.downFlow = downFlow;
    }

    public long getSumFlow() {
        return sumFlow;
    }

    public void setSumFlow(long sumFlow) {
        this.sumFlow = sumFlow;
    }
    public void setSumFlow() {
        this.sumFlow = this.upFlow + this.downFlow;
    }

    @Override
    public int compareTo(FlowBean o) {
        // 对总流量sumFlow倒序排序--递减排序
        if(this.sumFlow > o.sumFlow){
            return -1;
        } else if (this.sumFlow < o.sumFlow) {
            return 1;
        }else{
            //区内排序--即 二次排序
            //总流量一样的时候 对上行流量 upFlow正序排序--递增排序
            if(this.upFlow > o.upFlow){
                return 1;
            } else if (this.upFlow < o.upFlow) {
                return -1;
            }
            return 0;
        }
    }
}
